<?php
/**
 * 修改分享的提交页面
 * 
 * @author zhangchao
 * @since 2012-05-15
 */

include_once($_SERVER["DOCUMENT_ROOT"] . "/core/inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/core/admin.inc.php");
include_once($_SERVER["DOCUMENT_ROOT"] . "/include/foodv2/inc.php");

$g_cgival = array();
$g_pro = array();
$g_show = array();


function check_cgi_pro()
{
	global $g_cgival, $g_pro, $g_show;
	
	//check refer  -  add by xinwei
	if(! security_check::check_refer())
	{
		throw new Exception("来路不明");
	}
	
	cgi::both($g_cgival['status_id'], 'status_id', '');
	$g_cgival['status_id'] = trim($g_cgival['status_id']);
	if($g_cgival['status_id'] == 0 || !is_numeric($g_cgival['status_id']))
	{
		throw new Exception('status_id不合法');
	}
	
	cgi::both($g_cgival["tag_name"], "tag_name", "");
	$g_cgival['tag_name'] = string::un_html(string::un_script_code(mod_status_core::sbc_to_dbc($g_cgival['tag_name'])));
	
	$tag_list = array();
	$tag = explode(" ",trim($g_cgival["tag_name"]));
	for ($i=0; $i<count($tag); $i++)
	{
		if (!empty($tag[$i]))
		{
			$tag_list[] = $tag[$i];
			if (!preg_match("/^([\x{4e00}-\x{9fa5}0-9A-Za-z_\-]+)$/u",$tag[$i]))
			{
					throw new Exception('1002:微博标签仅支持中英文、数字、"_"或减号', 0);
			}
			if (string_utf8::string_len($tag[$i])>7)
			{
				throw new Exception("标签内容超过7个字");
				break;
			}
		}
	}
	//删除重复
	$tag_list = array_unique($tag_list);

	if (count($tag_list)>5)
	{
		throw new Exception("最多添加5个标签");
	}
	$g_cgival["tag_name"] = $tag_list;
	
	
	cgi::both($g_cgival["is_res"], "is_res", 0);
	$g_cgival["is_res"] = intval($g_cgival["is_res"]);
	
	cgi::both($g_cgival["res_id"], "res_id", 0);
	$g_cgival["res_id"] = intval($g_cgival["res_id"]);
	
	cgi::both($g_cgival["status"], "status", 0);
	$g_cgival["status"] = intval($g_cgival["status"]);
}

function get_data()
{
	global $g_cgival, $g_pro, $g_show,$config;
	
	$site_id = admin::admin_get_user_site_id();
	$admin_id = admin::admin_get_login_uid();
	$admin_name = admin::admin_get_login_user_name();
	
	$db_food_status = new db_food_status();
	$status_info_tmp = $db_food_status->admin_get_status_search_by_status_id($g_cgival['status_id']);
	if(empty($status_info_tmp))
	{
		throw new Exception('分享不存在或已被删除');
	}
	else
	{
		$status_info = $status_info_tmp[0];
		$result0 = $db_food_status->admin_update_status_by_status_id_full($site_id, $g_cgival['status_id'], $g_cgival["is_res"], $g_cgival["res_id"],$g_cgival["status"]);

		$db_food_status_tag = new db_food_status_tag();
		$db_food_status_tag->admin_delete_tag_by_status_id_tag($g_cgival['status_id']);
		foreach ($g_cgival["tag_name"] as $key=>$tag) {
			$db_food_status_tag->admin_insert_food_status_tag($site_id, $g_cgival['status_id'], $tag, $g_cgival["is_res"], $status_info['love_hot'], $status_info['is_pic'], count($g_cgival["tag_name"])-1-$key);
		}
		
		$admin_log = new admin_log();
		$admin_log->admin_insert_log_info($admin_id, $site_id, 2, "编辑 美食分享 status_id={$g_cgival['status_id']}");
	}
}

try
{
	admin_check_login();
// 	admin_check_user_priv("foodv2/album_cover_template_add_post.php");
	
	check_cgi_pro();
	get_data();
	$g_show["error"] = 0;
	$g_show["errmsg"] = '';
}
catch(Exception $e)
{
	$g_show["error"] = 1;
	$g_show["errmsg"] = $e->getMessage();
}

show_pro();
exit;


function show_pro()
{
	global $g_cgival, $g_pro, $g_show;
	if(isset($_GET['debug']))
		print_r($g_show);
	echo output::json('json',$g_show);
}

?>